package com.xnyzc.lhy.mis.service.impl.worksheet;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.entity.DiDiRv;
import com.xnyzc.lhy.common.exception.WorksheetException;
import com.xnyzc.lhy.common.penum.worksheet.WorksheetErrorCode;
import com.xnyzc.lhy.mis.entity.worksheet.OaSysWorksheet;
import com.xnyzc.lhy.mis.mapper.worksheet.OaSysWorksheetMapper;
import com.xnyzc.lhy.mis.service.worksheet.IOaSysWorksheetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 工单信息表 服务实现类
 * </p>
 *
 * @author cuixiaojian
 * @since 2019-11-04
 */
@Service
@Slf4j
public class OaSysWorksheetServiceImpl extends ServiceImpl<OaSysWorksheetMapper, OaSysWorksheet> implements IOaSysWorksheetService {

    @Autowired
    OaSysWorksheetMapper oaSysWorksheetMapper;

    @Override
    public DiDiRv queryWorksheetByHeliWorksheetId(Long heliWorksheetId) {
        OaSysWorksheet oaSysWorksheet = oaSysWorksheetMapper.selectOne(
                Wrappers.<OaSysWorksheet>lambdaQuery()
                        .eq(OaSysWorksheet::getHeliWorksheetId, heliWorksheetId)
        );
     return DiDiRv.wrapSuccess(oaSysWorksheet);
    }

    @Override
    public DiDiRv queryWorksheetByDiDiWorksheetId(Long didiWorksheetId) {
        OaSysWorksheet oaSysWorksheet = oaSysWorksheetMapper.selectOne(
                Wrappers.<OaSysWorksheet>lambdaQuery()
                        .eq(OaSysWorksheet::getDidiWorksheetId, didiWorksheetId)
        );
        return DiDiRv.wrapSuccess(oaSysWorksheet);
    }

    /**
     * 根据合力工单id获取工单信息
     * @param heliWorksheetId
     * @return
     */
    @Override
    public OaSysWorksheet queryWorksheetByHeliWorksheetId(String heliWorksheetId) {

        try {
            QueryWrapper<OaSysWorksheet> queryWrapper = new QueryWrapper<>();

            queryWrapper.eq(OaSysWorksheet.HELI_WORKSHEET_ID, heliWorksheetId);
            List<OaSysWorksheet> worksheetList = oaSysWorksheetMapper.selectList(queryWrapper);
            if (worksheetList.isEmpty()) {
                throw WorksheetException.create(WorksheetErrorCode.getWorksheetFailed);
            }
            else {
                return worksheetList.get(0);
            }
        }catch (Exception ex){
            log.info("查询工单异常");
            throw WorksheetException.create(WorksheetErrorCode.getWorksheetFailed);
        }

    }

    /**
     * 创建工单数据
     * @param oaSysWorksheet
     */
    @Override
    public void insertOaSysWorksheet(OaSysWorksheet oaSysWorksheet){
        oaSysWorksheetMapper.insert(oaSysWorksheet);
    }

    /**
     * 更新工单
     * @param oaSysWorksheet
     */
    @Override
    public void updateDiDiWorkSheetId(OaSysWorksheet oaSysWorksheet) {
        UpdateWrapper<OaSysWorksheet> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(OaSysWorksheet.DIDI_WORKSHEET_ID, oaSysWorksheet.getDidiWorksheetId());
        updateWrapper.eq(OaSysWorksheet.WORKSHEET_ID, oaSysWorksheet.getWorksheetId());
        oaSysWorksheetMapper.update(null, updateWrapper);
    }
}
